import 'package:flutter/material.dart';

// 程序入口，启动 Flutter 应用
void main() => runApp(const MyApp());

// 应用的主类，继承 StatelessWidget，负责配置主题和首页
class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '计数器与导航示例',
      theme: ThemeData(primarySwatch: Colors.amber),
      home: const MyHomePage(title: '计数器导航主页'),
    );
  }
}

// 主页面，使用 StatefulWidget 管理计数器和底部导航状态
class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0; // 计数器值
  int _selectedIndex = 0; // 当前选中的底部导航索引

  // 页面标题列表，与底部导航对应
  static const List<String> _titles = ['主页', '商务', '校园'];

  // 页面内容列表，与底部导航对应
  static const List<Widget> _simplePages = <Widget>[
    Center(child: Text('主页内容', style: TextStyle(fontSize: 26))),
    Center(child: Text('商务内容', style: TextStyle(fontSize: 26))),
    Center(child: Text('校园内容', style: TextStyle(fontSize: 26))),
  ];

  // 增加计数器并显示反馈
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
    ScaffoldMessenger.of(
      context,
    ).showSnackBar(const SnackBar(content: Text('计数器已增加！')));
  }

  // 处理底部导航栏点击事件，切换页面
  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    final Widget bodyContent = _simplePages[_selectedIndex];

    return Scaffold(
      appBar: AppBar(
        title: Text(_titles[_selectedIndex]),
        centerTitle: true,
        actions: [
          // 添加刷新按钮
          IconButton(
            icon: const Icon(Icons.refresh),
            tooltip: '刷新',
            onPressed: () {
              ScaffoldMessenger.of(
                context,
              ).showSnackBar(const SnackBar(content: Text('点击了刷新按钮 - AppBar')));
            },
          ),
        ],
      ),
      drawer: const MyDrawer(),
      bottomNavigationBar: BottomNavigationBar(
        items: const [
          BottomNavigationBarItem(icon: Icon(Icons.home_outlined), label: '主页'),
          BottomNavigationBarItem(icon: Icon(Icons.work_outline), label: '商务'),
          BottomNavigationBarItem(
            icon: Icon(Icons.school_outlined),
            label: '校园',
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.amber,
        onTap: _onItemTapped,
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: '增加计数',
        child: const Icon(Icons.add_circle_outline),
      ),
      body: Stack(
        children: [
          // 页面主体内容
          Positioned.fill(child: bodyContent),
          // 计数器区域，根据页面索引动态定位
          Align(
            alignment: () {
              if (_selectedIndex == 1) return Alignment.topLeft;
              if (_selectedIndex == 2) return Alignment.topRight;
              return Alignment.center;
            }(),
            child: Container(
              margin: const EdgeInsets.all(16),
              padding: const EdgeInsets.all(12),
              decoration: BoxDecoration(
                color: Colors.amber.shade100,
                borderRadius: BorderRadius.circular(10),
                boxShadow: [
                  BoxShadow(
                    color: Colors.amber.shade300.withOpacity(0.4),
                    offset: const Offset(3, 3),
                    blurRadius: 6,
                  ),
                ],
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Padding(
                    padding: const EdgeInsets.only(left: 8, bottom: 4),
                    child: Text(
                      '点击次数：',
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                        color: Colors.amber.shade800,
                      ),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(
                      vertical: 6,
                      horizontal: 8,
                    ),
                    child: Text(
                      '$_counter',
                      style: TextStyle(
                        fontSize: 32,
                        fontWeight: FontWeight.bold,
                        color: Colors.amber.shade900,
                      ),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.fromLTRB(8, 0, 8, 8),
                    child: Text(
                      '点击按钮增加！',
                      style: TextStyle(
                        fontSize: 14,
                        color: Colors.amber.shade700,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

// 抽屉菜单组件，提供用户头像和菜单选项
class MyDrawer extends StatelessWidget {
  const MyDrawer({super.key});

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: MediaQuery.removePadding(
        context: context,
        removeTop: true,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 用户信息区域
            Padding(
              padding: const EdgeInsets.only(top: 40.0),
              child: Row(
                children: [
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 16.0),
                    child: ClipOval(
                      child: Image.asset(
                        'lib/c098609fae5337f.jpg', // 更新：使用相对于项目根目录的相对路径
                        width: 80,
                        height: 80,
                        fit: BoxFit.cover,
                      ),
                    ),
                  ),
                  const Text(
                    '李四',
                    style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
                  ),
                ],
              ),
            ),
            const Divider(),
            // 菜单列表
            Expanded(
              child: ListView(
                children: [
                  ListTile(
                    leading: const Icon(Icons.person_add_outlined),
                    title: const Text('添加账户'),
                    onTap: () {
                      Navigator.pop(context);
                      ScaffoldMessenger.of(context).showSnackBar(
                        const SnackBar(content: Text('点击了添加账户 - Drawer')),
                      );
                    },
                  ),
                  ListTile(
                    leading: const Icon(Icons.settings_applications),
                    title: const Text('账户管理'),
                    onTap: () {
                      Navigator.pop(context);
                      ScaffoldMessenger.of(context).showSnackBar(
                        const SnackBar(content: Text('点击了账户管理 - Drawer')),
                      );
                    },
                  ),
                  ListTile(
                    leading: const Icon(Icons.info_outline),
                    title: const Text('关于我们'),
                    onTap: () {
                      Navigator.pop(context);
                      ScaffoldMessenger.of(context).showSnackBar(
                        const SnackBar(content: Text('点击了关于我们 - Drawer')),
                      );
                    },
                  ),
                ],
              ),
            ),
            const Divider(),
            // 底部版权信息
            const Padding(
              padding: EdgeInsets.all(16.0),
              child: Text('© 2025 李四工作室', style: TextStyle(color: Colors.grey)),
            ),
          ],
        ),
      ),
    );
  }
}
